package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import Projektarbete.ContactTabListFiller;
import Projektarbete.Group;
import Projektarbete.User;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;

/**
 * 
 * @author Calle
 */
public class AddGroupMember extends javax.swing.JDialog implements
		ActionListener {

	private JButton addButton;
	private JList friendsList;
	private JScrollPane jScrollPane1;
	private Group group;
	private User user;
	private ArrayList<User> addableFriends;

	public AddGroupMember(java.awt.Frame parent, boolean modal, Group group,
			User user) {
		super(parent, modal);
		this.group = group;
		this.user = user;
		initComponents();
	}

	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		friendsList = new javax.swing.JList();
		addButton = new JButton("Add Members");

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		ContactTabListFiller filler = new ContactTabListFiller();
		ArrayList<User> friends = filler.getContactList(user);
		ArrayList<User> membersInGroup = group.getMembers();
		addableFriends = new ArrayList<User>();

		boolean isAlreadyInGroup = false;
		for (int i = 0; i < friends.size(); i++) {
			for (int j = 0; j < membersInGroup.size(); j++) {
				if (friends.get(i).getU_id() == membersInGroup.get(j).getU_id())
					isAlreadyInGroup = true;
			}
			if (!isAlreadyInGroup)
				addableFriends.add(friends.get(i));
			isAlreadyInGroup = false;
		}

		//friendsList.setFont(new java.awt.font)
		friendsList.setModel(new javax.swing.AbstractListModel() {
			public int getSize() {
				return addableFriends.size();
			}
			public Object getElementAt(int i) {
				return addableFriends.get(i).getUserName();
			}
		});
		jScrollPane1.setViewportView(friendsList);

		addButton.addActionListener(this);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(addButton).addContainerGap())
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										186,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(0, 0, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										262,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(addButton).addContainerGap()));

		pack();
	}

	public void actionPerformed(ActionEvent arg0) {
		group.addUser(addableFriends.get(friendsList.getSelectedIndex()));
		new ContactTabListFiller().updateGroup(group);
	}

}
